//
//  WUTaskController.m
//  Home
//
//  Created by 吴军虎吴军虎 on 16/6/2.<taoliang>
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "WUTaskController.h"
#import "WUTaskCell.h"
#import "WUEditViewController.h"
#import "YHYEditHomePageViewController.h"

@interface WUTaskController ()<UITableViewDelegate,UITableViewDataSource,WUTaskCellDelegate>
{
    
    NSMutableArray *myArray;
    ResignModel *model;
    MJRefreshGifHeader *hederRefresh;
}
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) UIButton  *button;


@end

@implementation WUTaskController

-(void) viewWillAppear:(BOOL)animated
{
    //    self.tabBarController.tabBar.hidden=YES;
    self.title=@"我的案例";
    //    [self getAFNetingUrl];
    [hederRefresh beginRefreshing];
    
    self.navigationController.navigationBar.translucent = NO;
}

-(void) viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    myArray = [NSMutableArray new];
    
    self.view.backgroundColor=LV;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"GOback"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.backgroundColor = GH;
    [self.view addSubview:_button];
    [self.button setTitle:@"添加案例" forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(goNext:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
        make.centerXWithinMargins.mas_equalTo(self.view);
        make.height.mas_equalTo(40 * AUTO_HEIGHT);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    self.button.titleLabel.font = [UIFont fontWithName:@"Arial" size:LOGINFONT * TEXTWEIGHT];
    
    
    _myTableView = [UITableView new];
    _myTableView.backgroundColor = LV;
    [self.view addSubview:_myTableView];
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0 * AUTO_HEIGHT);
        make.left.mas_equalTo(0 * AUTO_WIDTH);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-40 * AUTO_HEIGHT);
        
    }];
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    
    [_myTableView registerClass:[WUTaskCell class] forCellReuseIdentifier:@"WUTaskCell"];
    
    hederRefresh = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(UploadHeader)];
    hederRefresh.lastUpdatedTimeLabel.hidden = YES;
    hederRefresh.tintColor = [UIColor whiteColor];
    [hederRefresh setTintColor:[UIColor whiteColor]];
    hederRefresh.stateLabel.font = [UIFont fontWithName:ZIXING size:14];
    hederRefresh.stateLabel.textColor = [UIColor whiteColor];
    [hederRefresh setTitle:@"我的案例" forState:MJRefreshStateIdle];
    [hederRefresh setTitle:@"刷新更多信息" forState:MJRefreshStatePulling];
    [hederRefresh setTitle:@"加载更多数据" forState:MJRefreshStateRefreshing];
    
    _myTableView.mj_header = hederRefresh;
    [hederRefresh beginRefreshing];
    
}
-(void)UploadHeader{
    
    [self getAFNetingUrl];
    
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"00000%ld",myArray.count);
    return myArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *str =@"WUTaskCell";
    WUTaskCell *cell=[tableView dequeueReusableCellWithIdentifier:str forIndexPath:indexPath];
    cell.delegate=self;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    [cell settingModel:myArray[indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 760/3 * AUTO_HEIGHT;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WUEditViewController *edit=[[WUEditViewController alloc]init];
    edit.myID = [myArray[indexPath.row] markID];
    edit.model =  myArray[indexPath.row];
    [self.navigationController pushViewController:edit animated:YES];
}

-(void) Back:(UIButton *) btn
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) taskViewCell:(NSInteger) index
{
    if (index ==2) {
        
        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定删除任务" preferredStyle:UIAlertControllerStyleAlert];
        
        
        [alertControl addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            
            NSLog(@"确定按钮");
            
        }]];
        
        [alertControl addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        
        [self presentViewController:alertControl animated:YES completion:nil];
        
    }else{
        
        YHYEditHomePageViewController *add=[[YHYEditHomePageViewController alloc]init];
        [self.navigationController pushViewController:add animated:YES];
        
    }
    
}

-(void) goNext:(UIButton *) btn
{
    YHYEditHomePageViewController *add=[[YHYEditHomePageViewController alloc]init];
    [self.navigationController pushViewController:add animated:YES];
}
-(void)getAFNetingUrl{
    
    NSString *url_string = [NSString stringWithFormat:@"%@%@",JIAFANURL,@"Gafaer/Case/myCaseList"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = HTTPSET;
    [manager GET:url_string parameters:@{@"uid":[LOCALSEVE objectForKey:@"UID"],@"mcode":[LOCALSEVE objectForKey:@"MCODE"]} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"我的案例列表：%@",responseObject);
        if ([[responseObject objectForKey:@"code"] isEqualToString:@"203"]) {
            //            [myArray insertObject:@"" atIndex:myArray.count];
            // UIImageView *backImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"noMessage"]];
            //  backImageView.frame = CGRectMake(0, 80, KScreenWidth, 120);
            // [self.myTableView addSubview:backImageView];
            
        }
        else if ([[responseObject objectForKey:@"code"] isEqualToString:@"200"]){
            
            
            NSArray *array = [responseObject objectForKey:@"data"];
            [myArray removeAllObjects];
            for (NSMutableDictionary * dic  in array) {
                model = [ResignModel new];
                [model setValuesForKeysWithDictionary:dic];
                
                [myArray insertObject:model atIndex:myArray.count];
            }
            
        }
        else {
            
            //            [myArray insertObject:@"" atIndex:myArray.count];
            
            
        }
        
        
        [self.myTableView reloadData];
        [hederRefresh endRefreshing];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"error:%@",error);
        
    }];
    
    
    
}


@end
